<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="小题名称" prop="smallTopic">
        <el-input
          v-model="queryParams.smallTopic"
          placeholder="请输入小题名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="所属题库" prop="bankTypeId">
        <el-select
          v-model="queryParams.bankTypeId"
          filterable
          allow-create
          default-first-option
          placeholder="请选择所属题库"
          clearable
          :style="{width: '100%'}"
        >
          <el-option
            v-for="(item, index) in questionBankType"
            :key="index"
            :label="item.name"
            :value="item.id"
            :disabled="item.disabled"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="题目类型" prop="type">
        <el-input
          v-model="queryParams.type"
          disabled
          placeholder="请输入小题名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="选项数量" prop="optionCount">
        <el-input
          v-model="queryParams.optionCount"
          placeholder="请输入选项数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="答案(选择题/判断题可能会有)" prop="answer">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.answer"-->
      <!--          placeholder="请输入答案(选择题/判断题可能会有)"-->
      <!--          clearable-->
      <!--          size="small"-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="分数" prop="fraction">
        <el-input
          v-model="queryParams.fraction"
          placeholder="请输入分数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级知识点" prop="knowledgeA">
        <el-input
          v-model="queryParams.knowledgeA"
          placeholder="请输入一级知识点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二级知识点" prop="knowledgeB">
        <el-input
          v-model="queryParams.knowledgeB"
          placeholder="请输入二级知识点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="考察知识点" prop="investigateKnow">
        <el-input
          v-model="queryParams.investigateKnow"
          placeholder="请输入考察知识点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="认知分类" prop="categorization">
        <el-input
          v-model="queryParams.categorization"
          placeholder="请输入认知分类"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="难易程度" prop="difficulty">
        <el-select
          v-model="queryParams.difficulty"
          filterable
          allow-create
          default-first-option
          placeholder="请选择难易程度"
          clearable
          :style="{width: '100%'}"
        >
          <el-option
            v-for="(item, index) in field103Options"
            :key="index"
            :label="item.label"
            :value="item.value"
            :disabled="item.disabled"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="内容相关性" prop="correlation">
        <el-input
          v-model="queryParams.correlation"
          placeholder="请输入内容相关性"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板分类" prop="questionTemplateId">
        <el-select
          v-model="queryParams.questionTemplateId"
          filterable
          allow-create
          default-first-option
          placeholder="请选择模板分类"
          clearable
          :style="{width: '100%'}"
        >
          <el-option
            v-for="(item, index) in formworkOptions"
            :key="index"
            :label="item.label"
            :value="item.id"
            :disabled="item.disabled"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          clearable
          size="small"
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="questionbankList"
       :cell-class-name="tableRowClass"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!--      <el-table-column label="创建人" align="center" prop="id" />-->
      <el-table-column label="大题名称" align="center" :show-overflow-tooltip="true" prop="bigQuestion" />
      <el-table-column label="小题名称" align="center" :show-overflow-tooltip="true" prop="smallTopic" />
      <el-table-column label="题目类型" align="center" prop="type" />
      <el-table-column label="选项数量" align="center" prop="optionCount" />
      <el-table-column label="答案" align="center" :show-overflow-tooltip="true" prop="answer" />
      <el-table-column label="分数" align="center"  prop="fraction" />
      <el-table-column label="一级知识点" align="center" :show-overflow-tooltip="true" prop="knowledgeA" />
      <el-table-column label="二级知识点" align="center" :show-overflow-tooltip="true" prop="knowledgeB" />
      <el-table-column label="考察知识点" align="center" :show-overflow-tooltip="true" prop="investigateKnow" />
      <el-table-column label="认知分类" align="center" :show-overflow-tooltip="true" prop="categorization" />
      <el-table-column label="难易程度" align="center" :show-overflow-tooltip="true" prop="difficulty" />
      <el-table-column label="内容相关性" align="center" :show-overflow-tooltip="true" prop="correlation" />
      <el-table-column label="模板分类" align="center" :show-overflow-tooltip="true" prop="formwork" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updateBy" />
      <el-table-column
        label="操作"
        align="center"
        width="180px"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleShow(scope.row)"
            v-hasPermi="['topic:oneselect:show']"
          >预览</el-button>
          <el-button
            size="mini"
            type="text"
            :disabled="scope.row.templatkey"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['topic:oneselect:edit']"
          >选择</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 预览 -->
    <el-dialog
      :visible.sync="Showdetail"
      v-if="Showdetail"
      width="40%"
      append-to-body
      class="dialog"
    >
      <div>
        <showDetail :row="propdata"></showDetail>
      </div>
    </el-dialog>
    <!-- 添加或修改基本题库管理对话框 -->
  </div>
</template>

<script>
import {
  listQuestionbank,
  getQuestionbank,
  delQuestionbank,
  addQuestionbank,
  updateQuestionbank,
  exportQuestionbank,
} from "@/api/topic/questionbank";
import { search } from "@/api/topic/showDetail";
import showDetail from "@/components/showDetail";
import { listTemplate, getTemplate, delTemplate, addTemplate, updateTemplate, exportTemplate } from "@/api/topic/template";
import { listQuestionBankType} from "@/api/topic/QuestionBankType";
export default {
  name: "oneselect",
  components: { showDetail: showDetail },
   props: ["row"],
  data() {
    return {
      componentdata:null,
      propdata: [],
      delkey:null,
      deleitemIndex:null,
      num: 1,
      myaddkey: 0,
      formworkOptions: [],
      formData: {
        type: "单选题",
        explains:null,
        num:null,
        bankTypeId:null,
        fraction: undefined,
        beanName: "singleChoiceServiceImpl",
        difficulty: undefined,
        smallTopic: undefined,
        formwork:null,
        optionCount: 0,
        field106: undefined,
        answer: undefined,
        investigateKnow: undefined,
        knowledgeA: undefined,
        knowledgeB: undefined,
        options: [],
      },
      dialogrules: {
        difficulty: [
          {
            required: true,
            message: "选择题目难度",
            trigger: "blur",
          },
        ],
        type: [
          {
            required: true,
            message: "请输入题目类型",
            trigger: "blur",
          },
        ],
        bankTypeId:[
          {
            required: true,
            message: "请选择所属题库",
            trigger: "blur",
          },
        ],
        explains: [
          {
            required: true,
            message: "请输入小题讲解",
            trigger: "blur",
          },
        ],
        formwork:[
          {
            required: true,
            message: "请输入或选择模板分类",
            trigger: "blur",
          },
        ],
        fraction: [
          {
            required: true,
            message: "请输入分值,必须是数字",
            trigger: "blur",
          },
        ],
        field103: [
          {
            required: true,
            message: "请输入难易程度",
            trigger: "change",
          },
        ],
        smallTopic: [
          {
            required: true,
            message: "请输入小题名称",
            trigger: "blur",
          },
        ],
        answer: [
          {
            required: true,
            message: "请选择答案",
            trigger: "blur",
          },
        ],
        field110: [
          {
            required: true,
            message: "请输入考察知识点",
            trigger: "blur",
          },
        ],
        field111: [
          {
            required: true,
            message: "请输入一级知识点",
            trigger: "blur",
          },
        ],
        field112: [
          {
            required: true,
            message: "请输入二级知识点",
            trigger: "blur",
          },
        ],
        field113: [
          {
            required: true,
            message: "请输入认知分类",
            trigger: "blur",
          },
        ],
        field114: [
          {
            required: true,
            message: "请输入内容相关性",
            trigger: "blur",
          },
        ],
      },
      field103Options: [
        {
          label: "1",
          value: 1,
        },
        {
          label: "2",
          value: 2,
        },
        {
          label: "3",
          value: 3,
        },
        {
          label: "4",
          value: 4,
        },
        {
          label: "5",
          value: 5,
        },
        {
          label: "6",
          value: 6,
        },
        {
          label: "7",
          value: 7,
        },
        {
          label: "8",
          value: 8,
        },
        {
          label: "9",
          value: 9,
        },
        {
          label: "10",
          value: 10,
        },
      ],
      options: [],
      // 题库
      questionBankType:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 基本题库管理表格数据
      questionbankList: [],
      // 弹出层标题
      title: "",
      // 是否显示预览内容
      Showdetail: false,
      // 是否显示弹出层
      open: false,
      // 题目类型字典
      typeOptions: [],
      // 认知分类字典
      categorizationOptions: [],
      //上个页面已经存在的题目数组
      excitem:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bigQuestion: null,
        smallTopic: null,
        beanName: "",
        optionCount: null,
        bankTypeId:null,
        answer: null,
        fraction: null,
        knowledgeA: null,
        knowledgeB: null,
        investigateKnow: null,
        categorization: null,
        questionTemplateId:null,
        difficulty: null,
        correlation: null,
        formwork: null,
        createTime: null,
        createBy: null,
        type:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.excitem=this.row.exitem
      this.queryParams.beanName=this.row.searchitem.beanName
      this.queryParams.questionTemplateId=this.row.searchitem.questionTemplateId
      // this.queryParams.type=this.row.searchitem.type
    this.getList();
    this.getFormarkdata();
    this.getDicts("question_type").then((response) => {
      this.typeOptions = response.data;
    });
    this.getDicts("topic_categorization").then((response) => {
      this.categorizationOptions = response.data;
    });
  },
  methods: {
    //选择模板
    aaa(){
      console.log(this.queryParams.formwork)
    },
    //点击选择按钮
    handleUpdate(row){
     this.$confirm("添加该小题?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
         const data = {
         bankId: row.id,
         beanName: row.beanName,
        };
        search(data).then((response) => {
         this.componentdata=response
         this.$emit("componentdata", this.componentdata);
        })
      })
       .catch(()=>{})


    },
    //提交模板类型
    handleFormwork(){
       for (let i = 0; i <this.formworkOptions.length; i++) {
          if(this.formworkOptions[i].value ==this.formData.formwork){
            this.formData.optionCount=this.formworkOptions[i].count
            this.formData.fraction=this.formworkOptions[i].questionScore
          }
         }
    },
    //获取模板类型
    getFormarkdata() {
      this.loading = true;
      const data = {
        beanName:this.row.searchitem.beanName,
      };
       listTemplate(data).then(response => {
         this.formworkOptions=response.rows
         for (let i = 0; i < response.rows.length; i++) {
          this.formworkOptions[i].label=response.rows[i].name
          this.formworkOptions[i].value=response.rows[i].name
         }

      });
    },
    //修改行颜色
    tableRowClass(record,index){
      console.log(record)
        if(record.row.templatkey ==true){
          return "rowClass";
        }
      return "";
    },
    /** 查询基本题库管理列表 */
    getList() {
      this.loading = true;
      listQuestionbank(this.queryParams).then((response) => {
        //去重
        let list=response.rows//获取到的所有小题
         console.log("未去重",response.rows)
         console.log("上个页面已存在",this.excitem)//上个页面已存在的小题

          for (var i = 0 ; i < response.rows.length ; i ++ ){
              response.rows[i].templatkey=false
          if(this.excitem.length!=0){
            for(var j = 0 ; j < this.excitem.length ; j ++ ){
             if (response.rows[i].id == this.excitem[j].id){
                //  response.rows.splice(i,1); //利用splice函数删除元素，从第i个位置，截取长度为1的元素
                 response.rows[i].templatkey=true

                }
                }
              }
             console.log("标记后",response.rows)
         }
        this.questionbankList=response.rows
        this.total = response.total;
        this.loading = false;
      });
      listQuestionBankType().then(response => {
        this.questionBankType=response.rows
      });
    },
    // 题目类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 认知分类字典翻译
    categorizationFormat(row, column) {
      return this.selectDictLabel(
        this.categorizationOptions,
        row.categorization
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        personId: null,
        bigQuestion: null,
        smallTopic: null,
        type: null,
        optionCount: null,
        answer: null,
        fraction: null,
        knowledgeA: null,
        knowledgeB: null,
        investigateKnow: null,
        categorization: null,
        difficulty: null,
        correlation: null,
        formwork: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 预览按钮操作 */
    handleShow(row) {
      this.propdata = row;
      this.Showdetail = true;
    },

  },
};
</script>
<style>
.radio {
  display: flex;
  flex-direction: column;
}
.radio-item {
  margin-top: 10px;
}
.icon {
  display: inline;
  margin-left: 20px;
}
.rowClass{
   background: #DCDCDC;
}
</style>
